<div class="tutorial-bundle" data-metadata-json='{"programKind": "per-method", "difficulty": "beginner", "groupedProjectAssets": [{"name": "Cartoon fruit", "assets": ["Apple-1.png", "Apple-2.png", "Apple-3.png", "Apple-4.png", "GoldApple-1.png", "Lemon-1.png", "Lemon-2.png", "Lemon-3.png", "Lemon-4.png", "Orange-1.png", "Orange-2.png", "Orange-3.png", "Orange-4.png", "Strawberry-1.png"]}]}' data-tip-sha1="1c1e022f186325bfeb8f21ab24f3f6face033c91"><div class="front-matter" data-complete-code-text='import pytch
import random
import math


class Stage(pytch.Stage):
    Backdrops = ["leafy-background.png"]

    @pytch.when_green_flag_clicked
    def setup(self):
        Stage.score = 0
        pytch.show_variable(Stage, "score")

    @pytch.when_stage_clicked
    def missed_fruit(self):
        Stage.score -= 5
        if Stage.score &lt; 0:
            Stage.score = 0


class Fruit(pytch.Sprite):
    Costumes = [
        "Apple-1.png",
        "Apple-2.png",
        "Apple-3.png",
        "Apple-4.png",
        "GoldApple-1.png",
        "Lemon-1.png",
        "Lemon-2.png",
        "Lemon-3.png",
        "Lemon-4.png",
        "Orange-1.png",
        "Orange-2.png",
        "Orange-3.png",
        "Orange-4.png",
        "Strawberry-1.png"
    ]

    @pytch.when_green_flag_clicked
    def init_size(self):
        self.set_size(0.25)

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
        self.hide()

        Stage.score += 1

        pytch.wait_seconds(1)

        appear_x = random.randint(-200, 200)
        appear_y = random.randint(-140, 140)
        self.go_to_xy(appear_x, appear_y)

        new_costume = random.choice(
            ["Apple-1.png", "Orange-1.png"]
        )
        self.switch_costume(new_costume)

        self.show()
' data-initial-code-text='import pytch
import random
import math


class Stage(pytch.Stage):
    Backdrops = ["solid-white.png"]
'><h1>Script-by-script Shoot the fruit</h1><p>You're going to write a game in Python where the player has to click
on fruit to score points.  If you know some Scratch, you'll see that
in many ways Pytch is similar to Scratch.  But it's fine if you
haven't done any Scratch.</p><p><img alt="screenshot" src="screenshot.png#img-center"/></p></div><div class="chapter-content"><h2>Use a more interesting background</h2><p>Run the empty game now, by clicking the green play button.  You'll see
that not much happens.  The <em>Stage</em> has a <em>backdrop</em>, which is what
you're seeing, but the starting backdrop is just a white rectangle,
which is not very interesting.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add a more interesting background for your game.  There is one in
Pytch's media library with a pattern of green leaves which will work
well.  It's called <code>leafy-background.png</code>.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "add-medialib-appearances-entry", "actor": {"kind": "stage"}, "displayIdentifier": "leafy-background.png", "nItems": 1}' data-slug="add-leafy-Backdrop"></div>
</div></div><p>Try the game now.  You'll see that nothing has changed.  This is
because Pytch shows the Stage's <em>first</em> backdrop, which is still the
solid white rectangle.</p><div class="learner-task"><div class="learner-task-intro">
<p>Delete the <code>solid-white.png</code> backdrop from the Stage.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "delete-appearance", "actor": {"kind": "stage"}, "appearanceFilename": "solid-white.png"}' data-slug="remove-default-backdrop"></div>
</div></div><p>Now your game should use the new background.</p><div class="learner-task"><div class="learner-task-intro">
<p>Try it!  Click the green play button to run your game.  You should see
the leafy green background.</p>
</div></div></div><div class="chapter-content"><h2>Create the fruit</h2><p>Now you need a <em>sprite</em>, to be the fruit which the player has to shoot.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add a sprite called <code>Fruit</code> to your game.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "add-sprite", "name": "Fruit"}' data-slug="add-Fruit-sprite"></div>
</div></div><p>Next your sprite needs some <em>costumes</em>, to say what it should look
like.</p><div class="learner-task"><div class="learner-task-intro">
<p>From the media library, add the "Cartoon fruit" bundle of images as
costumes for your sprite.  The first costume, which the sprite will
use by default, is a smiling apple.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "add-medialib-appearances-entry", "actor": {"kind": "sprite", "name": "Fruit"}, "displayIdentifier": "Cartoon fruit", "nItems": 14}' data-slug="costumes-for-Fruit"></div>
</div></div><p>Now your game should include an apple.</p><div class="learner-task"><div class="learner-task-intro">
<p>Try it!  Click the green play button to run your game.  You should see
the leafy green background with an apple in the middle.</p>
<p><strong>What is wrong?</strong></p>
</div><div class="learner-task-help">
<p>The apple is much too big!</p>
</div></div><h3>Set the fruit's size</h3><p>The game would be much too easy if the apple was this big.  You need
to write some code to make the apple smaller.</p><p>In Pytch (like in Scratch), your program is made up of <em>scripts</em>.  A
script runs when some event happens, like when the green flag is
clicked to start the game.  This is a good time to set the apple's
size.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add a <em>when green flag clicked</em> script to your Fruit sprite.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "add-script", "path": {"actor": {"kind": "sprite", "name": "Fruit"}, "methodName": "init_size"}, "event": {"kind": "green-flag"}, "codeText": ""}' data-slug="add-empty-Fruit-init-size-script"></div>
</div></div><p>At the moment, this script is empty — it has no Python code in it.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add a line of Python code to your script which sets the Fruit's size
to something more sensible.</p>
</div><div class="learner-task-help">
<p>Making the Fruit one-quarter of its default size is probably about
right.  If you know Scratch, you might know that</p>
<pre><code class="language-scratch">set size to (25) %
</code></pre>
<p>will do this.  Look in the help to find out how to do the same thing
in Pytch.</p>
</div><div class="learner-task-help">
<p>The Pytch method you need is</p>
<pre><code class="language-python-expression">self.set_size()
</code></pre>
<p>with a number between the <code>()</code>s saying what size you want.</p>
</div><div class="learner-task-help">
<p>To say "one quarter of its default size", you can use the value
<code>0.25</code>.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Fruit"}, "methodName": "init_size"}, "event": {"kind": "green-flag"}, "oldCodeText": "", "newCodeText": "self.set_size(0.25)"}' data-slug="add-Fruit-init-size-script-body"></div>
</div></div><p>Now the Fruit should be a better size.</p><div class="learner-task"><div class="learner-task-intro">
<p>Try it!  Click the green play button to run your game.  You should see
a more sensibly-sized apple on the leafy background.</p>
</div></div></div><div class="chapter-content"><h2>Let the player click the fruit</h2><p>You need to make something happen when the player clicks on the Fruit
— it should disappear.  If you know Scratch, you might know to do
something like</p><pre><code class="language-scratch">when this sprite clicked
hide
</code></pre><p>As you've just seen when you wrote code to set the Fruit's size,
things work in a very similar way in Pytch.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add a script to the Fruit sprite which runs when the player clicks on
it.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "add-script", "path": {"actor": {"kind": "sprite", "name": "Fruit"}, "methodName": "hit_fruit"}, "event": {"kind": "clicked"}, "codeText": ""}' data-slug="add-Fruit-when-clicked-script"></div>
</div></div><p>Again, you only need one line of code in the new script.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add code to this script which makes the Fruit hide.</p>
</div><div class="learner-task-help">
<p>You can check the help to see what Pytch method will make the Fruit
hide.</p>
</div><div class="learner-task-help">
<p>The method you want is</p>
<pre><code class="language-python-expression">self.hide()
</code></pre>
<p>There is no more information you need to give, but you do still need
the <code>()</code>s.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Fruit"}, "methodName": "hit_fruit"}, "event": {"kind": "clicked"}, "oldCodeText": "", "newCodeText": "self.hide()"}' data-slug="hide-when-hit"></div>
</div></div><p>It's a good idea to test your game after making each change.</p><div class="learner-task"><div class="learner-task-intro">
<p>Try it!  Click the green play button to run your game, then:</p>
<ul>
<li>On purpose, miss the apple by clicking on the background.  The apple
  should <em>not</em> disappear.</li>
<li>Click on the apple.  It should disappear.</li>
</ul>
</div></div></div><div class="chapter-content"><h2>Make the apple reappear after being hit</h2><p>The game is not very exciting yet.  Once the player clicks on the
apple, it disappears, and that's it.  You need to make the apple
reappear so the player can keep playing.</p><p>After the Fruit has hidden itself, it should wait for a short time,
and then show itself again.  If you know Scratch, you might add these
blocks to the bottom of the 'when this sprite clicked' script:</p><pre><code class="language-scratch">wait [1] seconds
show
</code></pre><p>In Python, you add two lines of code to your script.</p><div class="learner-task"><div class="learner-task-intro">
<p>Use the help to find what Pytch statements will:</p>
<ul>
<li>Make the script wait for one second.</li>
<li>Make the apple show itself.</li>
</ul>
</div><div class="learner-task-help">
<p>To wait for some amount of time, you can use the</p>
<pre><code class="language-python-expression">pytch.wait_seconds()
</code></pre>
<p>method, and put the number of seconds you want to wait between the
<code>()</code>s.  Notice how this code uses <code>pytch.</code> at the start.</p>
</div><div class="learner-task-help">
<p>To make a sprite show itself, you can use the</p>
<pre><code class="language-python-expression">self.show()
</code></pre>
<p>method.  You don't need anything between the <code>()</code>s, but the <code>()</code>s do
need to be there.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Fruit"}, "methodName": "hit_fruit"}, "event": {"kind": "clicked"}, "oldCodeText": "self.hide()", "newCodeText": "self.hide()\npytch.wait_seconds(1)\nself.show()"}' data-slug="wait-then-show"></div>
</div></div><p>Again, it's a good idea to test your game as you go along.</p><div class="learner-task"><div class="learner-task-intro">
<p>Try it!  Click the green play button to run your game, then:</p>
<ul>
<li>Click on the apple.  It should disappear, then after a second,
  re-appear.  Click it a few more times to make sure.</li>
</ul>
</div></div></div><div class="chapter-content"><h2>Make the apple reappear somewhere else</h2><p>The game is better now, but still not very exciting.  The player knows
where the apple is going to appear.  We want the apple to reappear at
a random place on on the Stage.</p><p>This chapter is quite long.  Go carefully!</p><h3>Saying where to go on the stage</h3><p>Positions on the Stage are described by two numbers, <code>x</code> and <code>y</code>:</p><ul>
<li>The <code>x</code> number says how far right of the centre we are (and so <code>x</code>
  is negative if we are <em>left</em> of the centre).</li>
<li>The left-right centre of the Stage is <code>x = 0</code>, far left is <code>x =
  -240</code>, and far right is <code>x = 240</code>.</li>
<li>The <code>y</code> number says how far above the centre we are (and so <code>y</code> is
  negative if we are <em>below</em> the centre).</li>
<li>The up-down centre of the Stage is <code>y = 0</code>, the very bottom is <code>y =
  -180</code>, and the very top is <code>y = 180</code>.</li>
</ul><p>If you know Scratch, you might notice that this is exactly the same
system Scratch uses.</p><div class="learner-task"><div class="learner-task-intro">
<p>Choose the <strong>Show coordinates</strong> entry from the drop-down menu at the
top right of the Stage.  Move the cross-hairs around the Stage and
check you're happy with how Pytch's <code>x</code> and <code>y</code> coordinates work.
Close the coordinate display when you're done.</p>
</div></div><h3>Picking a random number</h3><p>We'll need to randomly pick <code>x</code> and <code>y</code> numbers for where the apple
reappears.</p><div class="learner-task"><div class="learner-task-intro">
<p>Open the help, and look in the <em>Operators</em> section for the methods
which give you random numbers.  There is more than one.  Which do you
think is best for choosing random <code>x</code> and <code>y</code> coordinates?  Come back
to this tutorial when you've looked at this.</p>
</div></div><p>The <code>random.randint()</code> function will work well.  You need to give the
lowest and highest possible numbers.</p><div class="learner-task"><div class="learner-task-intro">
<p>Use the <strong>Show coordinates</strong> tool to choose the leftmost and rightmost
<code>x</code> coordinate, and the lowest and highest <code>y</code> coordinate, that the
apple should appear at.  Remember them or write them down!</p>
</div></div><p>These values will work, but you might want to adjust them:</p><ul>
<li>The random <code>x</code> coordinate should be between <code>-200</code> and <code>200</code>.</li>
<li>The random <code>y</code> coordinate should be between <code>-140</code> and <code>140</code>.</li>
</ul><h3>Pick a random place to appear</h3><p>The game needs to pick a random <code>x</code> number and a random <code>y</code> number for
where the apple should reappear.  Your code will store these values in
<em>variables</em>.  These work very much like Scratch variables.  In Python,
you don't need to explicitly 'make a variable' — you just set a
variable to a value, and the variable is created for you.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add a line of code to the Fruit's <em>when this sprite clicked</em> script to
choose a random number between <code>-200</code> and <code>200</code> and set a variable
<code>appear_x</code> to this value.</p>
<p>Think about where this line of code should go!</p>
</div><div class="learner-task-help">
<p>The code should go after the <code>pytch.wait_seconds(1)</code> line and before
the <code>self.show()</code> line, because the code should choose a position to
appear just before showing the Fruit.</p>
</div><div class="learner-task-help">
<p>Check the help for <em>Working with variables</em> to see how to set a
variable to a value.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Fruit"}, "methodName": "hit_fruit"}, "event": {"kind": "clicked"}, "oldCodeText": "self.hide()\npytch.wait_seconds(1)\nself.show()", "newCodeText": "self.hide()\npytch.wait_seconds(1)\n\nappear_x = random.randint(-200, 200)\n\nself.show()"}' data-slug="assign-new-x"></div>
</div></div><p>You need to do something similar for the <code>y</code> coordinate.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add another line of code which chooses a random number between <code>-140</code>
and <code>140</code>, and sets a variable <code>appear_y</code> to the chosen value.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Fruit"}, "methodName": "hit_fruit"}, "event": {"kind": "clicked"}, "oldCodeText": "self.hide()\npytch.wait_seconds(1)\n\nappear_x = random.randint(-200, 200)\n\nself.show()", "newCodeText": "self.hide()\npytch.wait_seconds(1)\n\nappear_x = random.randint(-200, 200)\nappear_y = random.randint(-140, 140)\n\nself.show()"}' data-slug="assign-new-y"></div>
</div></div><h3>Appear at the random location</h3><p>Now the code has randomly chosen where the apple should reappear, you
can add the line of code to send the apple there just before showing
itself.</p><p>If you know Scratch, you might have used variables to 'fill in holes'
in blocks, like:</p><pre><code class="language-scratch">go to x: (appear_x) y: (appear_y)
</code></pre><p>You can do the same thing in Python.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add a line of code which makes the Fruit go to the random position
just chosen.</p>
</div><div class="learner-task-help">
<p>Check the help to find out how to send a sprite to a position on the
Stage.</p>
</div><div class="learner-task-help">
<p>The method</p>
<pre><code class="language-python-expression">self.go_to_xy()
</code></pre>
<p>is what you want.  In the <code>()</code>s, you need to give expression for the
<code>x</code> and <code>y</code> coordinates, separated by a comma (<code>,</code>).</p>
</div><div class="learner-task-help">
<p>Remember that the <code>x</code> coordinate to go to is stored in the variable
<code>appear_x</code>, and the <code>y</code> coordinate in the variable <code>appear_y</code>.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Fruit"}, "methodName": "hit_fruit"}, "event": {"kind": "clicked"}, "oldCodeText": "self.hide()\npytch.wait_seconds(1)\n\nappear_x = random.randint(-200, 200)\nappear_y = random.randint(-140, 140)\n\nself.show()", "newCodeText": "self.hide()\npytch.wait_seconds(1)\n\nappear_x = random.randint(-200, 200)\nappear_y = random.randint(-140, 140)\nself.go_to_xy(appear_x, appear_y)\n\nself.show()"}' data-slug="go-to-random-x-y"></div>
</div></div><h3>Try it!</h3><div class="learner-task"><div class="learner-task-intro">
<p>Run your program with the green flag.  Every time you click on the
Fruit, it should disappear, then after a second, reappear in a random
place.</p>
</div></div></div><div class="chapter-content"><h2>Show the player's score</h2><p>It would be good if the player knew how well they were doing.  The
game should keep a <em>score</em>, and give the player a point every time
they shoot a Fruit.</p><h3>Create a variable to hold the score</h3><p>The game should set the score to zero at the start of the game, so
your program needs a script which runs when the game starts.</p><p>The score is something which the Fruit and the Stage both need to work
with.  There's not a very strong reason to set up the score in one or
the other.  This tutorial gives the job to the Stage.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add a script to the Stage which runs when the game starts.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "add-script", "path": {"actor": {"kind": "stage"}, "methodName": "setup"}, "event": {"kind": "green-flag"}, "codeText": ""}' data-slug="add-Stage-setup-script"></div>
</div></div><p>The score variable needs to exist across all scripts, so we'll use a
variable which 'belongs to' the Stage.  The full details of how all
this works are beyond the scope of this tutorial, but for now you just
need to know that you talk about a variable called <code>score</code> belonging
to the Stage with the Python expression</p><pre><code class="language-python-expression">Stage.score
</code></pre><div class="learner-task"><div class="learner-task-intro">
<p>Add a line of code to the Stage's new script which sets the
<code>Stage.score</code> variable to the number zero.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "stage"}, "methodName": "setup"}, "event": {"kind": "green-flag"}, "oldCodeText": "", "newCodeText": "Stage.score = 0"}' data-slug="init-Stage-score"></div>
</div></div><h3>Showing the score</h3><p>In Scratch you tick a box to show a variable on the Stage.  In Pytch,
you write code to do this.  The code will go just after the code to
start the score off at zero.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add a line of code to show the <code>Stage.score</code> variable.</p>
</div><div class="learner-task-help">
<p>Look in the help to see what Pytch method does this.</p>
</div><div class="learner-task-help">
<p>Here, the 'owner' is <code>Stage</code>, and the variable's name is <code>"score"</code>.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "stage"}, "methodName": "setup"}, "event": {"kind": "green-flag"}, "oldCodeText": "Stage.score = 0", "newCodeText": "Stage.score = 0\npytch.show_variable(Stage, \"score\")"}' data-slug="show-score"></div>
</div></div><h3>Try it!</h3><div class="learner-task"><div class="learner-task-intro">
<p>Run your program with the green flag.  Check you see the score
displayed at the top-left.  Click on the Fruit a few times.</p>
<p><strong>What's wrong?</strong></p>
</div><div class="learner-task-help">
<p>The score doesn't change!</p>
</div></div><p>So far your program has no way for the player to get points, so the
score will be stuck at zero.  The next chapter will fix this.</p></div><div class="chapter-content"><h2>Give the player points</h2><p>To increase the player's score, in Scratch you might do</p><pre><code class="language-scratch">change [score v] by [1]
</code></pre><p>Python has the <code>+=</code> operator ('change by adding'), which works the
same.  For example, if your code had a variable <code>Stage.health</code>, the
code to add five to it would be</p><pre><code class="language-python">Stage.health += 5
</code></pre><div class="learner-task"><div class="learner-task-intro">
<p><strong>In the Fruit sprite</strong>, add a line of code to the script which runs
when the Fruit is clicked, making it so the player gets one point.</p>
<p>Think about where in the script the new line of code should go.</p>
</div><div class="learner-task-help">
<p>To get a point, you need to add one to the <code>Stage.score</code> variable.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Fruit"}, "methodName": "hit_fruit"}, "event": {"kind": "clicked"}, "oldCodeText": "self.hide()\npytch.wait_seconds(1)\n\nappear_x = random.randint(-200, 200)\nappear_y = random.randint(-140, 140)\nself.go_to_xy(appear_x, appear_y)\n\nself.show()", "newCodeText": "self.hide()\n\nStage.score += 1\n\npytch.wait_seconds(1)\n\nappear_x = random.randint(-200, 200)\nappear_y = random.randint(-140, 140)\nself.go_to_xy(appear_x, appear_y)\n\nself.show()"}' data-slug="award-point-when-hit"></div>
</div></div><p>You might have noticed that the code in this tutorial sometimes has
blank lines.  Blank lines are ignored by Python, but they make the
code easier for a human to read.</p><h3>Try it!</h3><div class="learner-task"><div class="learner-task-intro">
<p>Run your program with the green flag.  Check the score goes up when
you click on the Fruit.</p>
</div></div></div><div class="chapter-content"><h2>Lose points if player misses</h2><p>The game would be more challenging if the player loses points when
they miss the Fruit.</p><p>Your program can react if the player misses by making the Stage run
some code when <em>it</em> is clicked.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add a <em>when stage clicked</em> script to the stage.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "add-script", "path": {"actor": {"kind": "stage"}, "methodName": "missed_fruit"}, "event": {"kind": "clicked"}, "codeText": ""}' data-slug="add-stage-clicked-script"></div>
</div></div><p>The code in this script should make the player lose some points.
Python has <code>-=</code> ('change by subtracting') for this.  For example, if
your program had a variable <code>Stage.health</code>, the code</p><pre><code class="language-python-expression">Stage.health -= 10
</code></pre><p>would subtract ten from it.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add a line of code to your script which subtracts five from the
<code>Stage.score</code> variable.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "stage"}, "methodName": "missed_fruit"}, "event": {"kind": "clicked"}, "oldCodeText": "", "newCodeText": "Stage.score -= 5"}' data-slug="lose-points-on-miss"></div>
</div></div><p>By now you should know what to do next!</p><div class="learner-task"><div class="learner-task-intro">
<p>Try it!  Run your program with the green flag.  Check the score goes
up when you click on the Fruit.  Check the score goes down when you
click on the Stage on purpose.</p>
<p><strong>What's the problem?</strong></p>
</div><div class="learner-task-help">
<p>If the score is less than five when the player misses, the score
becomes negative.</p>
</div></div><h3>Stop the score becoming negative</h3><p>If the first thing the player does is miss the Fruit, their score will
be <code>-5</code>.  This is not very sensible.</p><p>After subtracting five points, the code needs to test whether the
score has become negative.  If so, the score should be set to zero
instead.  To only run some code if some condition is met, Python has
the <code>if</code> statement.  If you know Scratch, it works the same as
Scratch's <code>if</code> block.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add an <code>if</code> statement under the <code>Stage.score += 5</code> line.  The <code>if</code>
statement should test whether <code>Stage.score</code> is negative, and, if so,
set <code>Stage.score</code> to zero.</p>
</div><div class="learner-task-help">
<p>The help gives some examples of how this works.  Look for <em>If/then</em> in
the <em>Control</em> section.  Notice how in Python, the <em>body</em> of the <code>if</code>
statement is <em>indented</em> — moved across from the left margin by four
spaces.</p>
<p>You need to work out the <em>test</em>, and work out what code goes in the
body of the <code>if</code> statement.</p>
</div><div class="learner-task-help">
<p>The test you want is</p>
<ul>
<li><em>Is the <code>Stage.score</code> variable less than zero?</em></li>
</ul>
<p>The Python expression for this is</p>
<pre><code class="language-python-expression">Stage.score &lt; 0
</code></pre>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "stage"}, "methodName": "missed_fruit"}, "event": {"kind": "clicked"}, "oldCodeText": "Stage.score -= 5", "newCodeText": "Stage.score -= 5\nif Stage.score &lt; 0:\n    Stage.score = 0"}' data-slug="clamp-score-at-zero"></div>
</div></div></div><div class="chapter-content"><h2>Game complete!</h2><p>This is now a playable game written in Python — congratulations!</p><div class="learner-task"><div class="learner-task-intro">
<p>Play a few games and see how quickly you can score points.</p>
</div></div></div><div class="chapter-content"><h2>Extra: Add another fruit</h2><p>To make the game look more interesting, you can add another kind of
fruit.</p><h3>Choose a random costume</h3><p>You have already used the Python function <code>random.randint()</code> to pick
random numbers for where the Fruit should appear on the screen.</p><p>The function</p><pre><code class="language-python-expression">random.choice()
</code></pre><p>chooses a random thing from a list.  You can use this to set a
variable to the name of a random costume for the Fruit to wear when it
reappears.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add code to the Fruit sprite's <em>when this sprite clicked</em> script.  The
code should:</p>
<ul>
<li>Choose a random costume, with the options being <code>"Apple-1.png"</code> and
  <code>"Orange-1.png"</code>.  You can check the <em>Costumes</em> tab to see what
  other costumes there are, if you prefer.</li>
<li>Set a variable <code>new_costume</code> to the chosen costume name.</li>
</ul>
</div><div class="learner-task-help">
<p>The help has an example for choosing from a list.  Look in the
<em>Operators</em> section for <code>random.choice()</code>.</p>
</div><div class="learner-task-help">
<p>You need to give <code>random.choice()</code> a <em>list</em> of options.  The Python
expression</p>
<pre><code class="language-python-expression">["Apple-1.png", "Orange-1.png"]
</code></pre>
<p>means a list with two things in it — the name <code>"Apple-1.png"</code> and the
name <code>"Orange-1.png"</code>.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Fruit"}, "methodName": "hit_fruit"}, "event": {"kind": "clicked"}, "oldCodeText": "self.hide()\n\nStage.score += 1\n\npytch.wait_seconds(1)\n\nappear_x = random.randint(-200, 200)\nappear_y = random.randint(-140, 140)\nself.go_to_xy(appear_x, appear_y)\n\nself.show()", "newCodeText": "self.hide()\n\nStage.score += 1\n\npytch.wait_seconds(1)\n\nappear_x = random.randint(-200, 200)\nappear_y = random.randint(-140, 140)\nself.go_to_xy(appear_x, appear_y)\n\nnew_costume = random.choice(\n    [\"Apple-1.png\", \"Orange-1.png\"]\n)\n\nself.show()"}' data-slug="choose-random-new-costume"></div>
<p><strong>Note on layout:</strong> Here, the new code is shown split into three
physical lines.  If you prefer, you can type it all as one line.</p>
</div></div><h3>Switch to the chosen costume</h3><p>If you know Scratch, you might have used the</p><pre><code class="language-scratch">switch costume to [Apple-1 v]
</code></pre><p>block to tell a sprite to use a different costume.  Pytch can do
the same.</p><div class="learner-task"><div class="learner-task-intro">
<p>After the <code>random.choice()</code> line, add a line of code which switches to
the chosen costume.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Fruit"}, "methodName": "hit_fruit"}, "event": {"kind": "clicked"}, "oldCodeText": "self.hide()\n\nStage.score += 1\n\npytch.wait_seconds(1)\n\nappear_x = random.randint(-200, 200)\nappear_y = random.randint(-140, 140)\nself.go_to_xy(appear_x, appear_y)\n\nnew_costume = random.choice(\n    [\"Apple-1.png\", \"Orange-1.png\"]\n)\n\nself.show()", "newCodeText": "self.hide()\n\nStage.score += 1\n\npytch.wait_seconds(1)\n\nappear_x = random.randint(-200, 200)\nappear_y = random.randint(-140, 140)\nself.go_to_xy(appear_x, appear_y)\n\nnew_costume = random.choice(\n    [\"Apple-1.png\", \"Orange-1.png\"]\n)\nself.switch_costume(new_costume)\n\nself.show()"}' data-slug="switch-to-random-costume"></div>
</div></div><p>And finally:</p><div class="learner-task"><div class="learner-task-intro">
<p>Try it!  Run your program.  Check you get a mixture of kinds of fruit.</p>
</div></div></div><div class="chapter-content"><h2>Challenges</h2><p>Can you change your program to solve these challenges?</p><ul>
<li>Choose from more costumes, not just an apple or an orange.</li>
<li>Give the player 10 points for every fruit they hit instead of 1.</li>
<li>Give the player 5 points for an apple and 10 points for an orange.
  (<em>Hint:</em> You can use the expression <code>self.costume_name</code> to find the
  name of the costume the sprite is currently wearing.)</li>
<li>Make the Fruit smaller, so it's harder to hit.</li>
<li><em>Harder challenge:</em> Make the Fruit smaller if the player has more
  points.  This makes the game get more difficult as the player gets
  better at it.</li>
</ul></div></div>